package cn.edu.cdp.dao;

import cn.edu.cdp.domain.Account;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface AccountDao {
    @Select("(select a.username, a.password, a.role, s.name " +
            "from account a, student s where a.username=s.student_id) " +
            "union " +
            "(select a.username, a.password, a.role, c.name " +
            "from account a, course c where a.username=c.course_id) " +
            "union " +
            "(select username, password, role, username name from account where role not in ('学生', '教师', '管理员'))")
    List<Map<String, Object>> allAccount();

    @Select("select username, password, role from account " +
            "where username=#{username} and password=#{password}")
    Account login(@Param("username") String username, @Param("password") String password);

    @Insert("insert into account (username, password, role) values (#{username}, #{password}, #{role})")
    void insert(Account account);

    @Update("update account set password=#{password}, role=#{role} where username=#{username}")
    void update(Account account);

    @Delete("delete from account where username=#{username}")
    void deleteByUsername(String username);
}
